{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-16T08:16:02.690541Z",
     "start_time": "2025-10-16T08:15:56.578440Z"
    }
   },
   "source": [
    "import json\n",
    "import requests\n",
    "\n",
    "from openai import OpenAI\n",
    "\n",
    "from tmp.Prompt_Caching101 import client\n",
    "\n",
    "MODEL_NAME = \"deepseek-r1:8b\"  # ollama list\n",
    "url = \"http://127.0.0.1:11434/api/generate\"\n",
    "payload = {\n",
    "    \"model\": MODEL_NAME,\n",
    "    \"prompt\": \"1+1=?\",\n",
    "    \"stream\": False,\n",
    "    \"options\": {\n",
    "        \"num_predict\": 10000,\n",
    "    },\n",
    "}\n",
    "\n",
    "completion = requests.post(url, json=payload)\n",
    "if completion.status_code == 200:\n",
    "    res_json = completion.json()\n",
    "    print(f\"res: {json.dumps(res_json, indent=2, ensure_ascii=False)}\")\n",
    "else:\n",
    "    print(f\"failed\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "res: {\n",
      "  \"model\": \"deepseek-r1:8b\",\n",
      "  \"created_at\": \"2025-10-16T08:16:02.692445575Z\",\n",
      "  \"response\": \"In basic arithmetic, 1 + 1 equals **2**. \\n\\nIs there a specific context (like binary or modular arithmetic) that you're referring to where this might change? Let me know if you'd like more details!\",\n",
      "  \"thinking\": \"First, the user asked \\\"1+1=?\\\" which is a simple math question. I know that 1 + 1 equals 2. That's basic arithmetic.\\n\\nBut maybe there's more to it? Sometimes users ask simple questions to test or see if the AI can handle them. Or perhaps they're referring to something else, like binary addition where 1+1=0 with a carry-over, but in that case, for integers, it should still be 2 unless specified otherwise.\\n\\nThe question is straightforward: \\\"1+1=?\\\" and no context is given. So, I should assume the standard decimal system unless indicated otherwise.\\n\\nIn programming or binary contexts, 1 + 1 might not always equal 2 if we're dealing with bitwise operations, but again, that seems unlikely here since it's a simple \\\"+\\\" operator without any special notation.\\n\\nAnother thing: in some mathematical systems or abstract concepts, like modular arithmetic, 1+1 could be different. For example, modulo 3, 1+1=2; modulo 4, still 2; only if the modulus is less than 2 would it change, but that doesn't make sense for integer addition.\\n\\nPerhaps they're thinking about concatenation or something else? Like in strings, \\\"1\\\" + \\\"1\\\" could be \\\"11\\\", but the question uses numbers and an equals sign, so probably not.\\n\\nThe user might be joking or testing. For instance, there's a meme where people say 1+1=window, referring to YouTube, but that seems outdated now with other platforms like TikTok.\\n\\nI should keep it simple and direct. Since this is likely a genuine question from someone just starting out or checking the basics, I'll go with the standard answer.\\n\\nBut as an AI, I need to be helpful and engaging. Maybe I can add some humor or explanation if appropriate.\\n\\nResponse: \\\"1 + 1 equals 2.\\\" That's clear.\\n\\nTo make it more interactive, I could say something like: \\\"In basic arithmetic, 1 + 1 equals 2. Is there a specific context you're thinking about?\\\"\\n\\nBut the question doesn't provide any context, so I should stick to the most common interpretation.\\n\\nAnother angle: in some contexts, like vector addition or set theory, but that seems overcomplicating for such a simple query.\\n\\nPerhaps it's about boolean logic? In binary, 1 + 1 = 2 (since both are true, and OR operation doesn't add, so probably not).\\n\\nI think I'm thinking too much. The user probably expects the answer to be 2.\\n\\nFinal decision: Answer directly with \\\"2\\\" but perhaps phrase it nicely.\\n\",\n",
      "  \"done\": true,\n",
      "  \"done_reason\": \"stop\",\n",
      "  \"context\": [\n",
      "    151669,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    19884,\n",
      "    151670,\n",
      "    151667,\n",
      "    198,\n",
      "    5338,\n",
      "    11,\n",
      "    279,\n",
      "    1196,\n",
      "    4588,\n",
      "    330,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    28,\n",
      "    7521,\n",
      "    892,\n",
      "    374,\n",
      "    264,\n",
      "    4285,\n",
      "    6888,\n",
      "    3405,\n",
      "    13,\n",
      "    358,\n",
      "    1414,\n",
      "    429,\n",
      "    220,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    16819,\n",
      "    220,\n",
      "    17,\n",
      "    13,\n",
      "    2938,\n",
      "    594,\n",
      "    6770,\n",
      "    34784,\n",
      "    382,\n",
      "    3983,\n",
      "    7196,\n",
      "    1052,\n",
      "    594,\n",
      "    803,\n",
      "    311,\n",
      "    432,\n",
      "    30,\n",
      "    17688,\n",
      "    3847,\n",
      "    2548,\n",
      "    4285,\n",
      "    4755,\n",
      "    311,\n",
      "    1273,\n",
      "    476,\n",
      "    1490,\n",
      "    421,\n",
      "    279,\n",
      "    15235,\n",
      "    646,\n",
      "    3705,\n",
      "    1105,\n",
      "    13,\n",
      "    2521,\n",
      "    8365,\n",
      "    807,\n",
      "    2299,\n",
      "    22023,\n",
      "    311,\n",
      "    2494,\n",
      "    770,\n",
      "    11,\n",
      "    1075,\n",
      "    7868,\n",
      "    5256,\n",
      "    1380,\n",
      "    220,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    28,\n",
      "    15,\n",
      "    448,\n",
      "    264,\n",
      "    6777,\n",
      "    28252,\n",
      "    11,\n",
      "    714,\n",
      "    304,\n",
      "    429,\n",
      "    1142,\n",
      "    11,\n",
      "    369,\n",
      "    25780,\n",
      "    11,\n",
      "    432,\n",
      "    1265,\n",
      "    2058,\n",
      "    387,\n",
      "    220,\n",
      "    17,\n",
      "    7241,\n",
      "    5189,\n",
      "    5937,\n",
      "    382,\n",
      "    785,\n",
      "    3405,\n",
      "    374,\n",
      "    30339,\n",
      "    25,\n",
      "    330,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    28,\n",
      "    7521,\n",
      "    323,\n",
      "    902,\n",
      "    2266,\n",
      "    374,\n",
      "    2661,\n",
      "    13,\n",
      "    2055,\n",
      "    11,\n",
      "    358,\n",
      "    1265,\n",
      "    9658,\n",
      "    279,\n",
      "    5297,\n",
      "    12122,\n",
      "    1849,\n",
      "    7241,\n",
      "    16317,\n",
      "    5937,\n",
      "    382,\n",
      "    641,\n",
      "    15473,\n",
      "    476,\n",
      "    7868,\n",
      "    37597,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    2578,\n",
      "    537,\n",
      "    2677,\n",
      "    6144,\n",
      "    220,\n",
      "    17,\n",
      "    421,\n",
      "    582,\n",
      "    2299,\n",
      "    14550,\n",
      "    448,\n",
      "    97970,\n",
      "    7525,\n",
      "    11,\n",
      "    714,\n",
      "    1549,\n",
      "    11,\n",
      "    429,\n",
      "    4977,\n",
      "    17367,\n",
      "    1588,\n",
      "    2474,\n",
      "    432,\n",
      "    594,\n",
      "    264,\n",
      "    4285,\n",
      "    59498,\n",
      "    5675,\n",
      "    2041,\n",
      "    894,\n",
      "    3281,\n",
      "    44197,\n",
      "    382,\n",
      "    14037,\n",
      "    3166,\n",
      "    25,\n",
      "    304,\n",
      "    1045,\n",
      "    35972,\n",
      "    5942,\n",
      "    476,\n",
      "    8115,\n",
      "    18940,\n",
      "    11,\n",
      "    1075,\n",
      "    43893,\n",
      "    34784,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    1410,\n",
      "    387,\n",
      "    2155,\n",
      "    13,\n",
      "    1752,\n",
      "    3110,\n",
      "    11,\n",
      "    59864,\n",
      "    220,\n",
      "    18,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    28,\n",
      "    17,\n",
      "    26,\n",
      "    59864,\n",
      "    220,\n",
      "    19,\n",
      "    11,\n",
      "    2058,\n",
      "    220,\n",
      "    17,\n",
      "    26,\n",
      "    1172,\n",
      "    421,\n",
      "    279,\n",
      "    74024,\n",
      "    374,\n",
      "    2686,\n",
      "    1091,\n",
      "    220,\n",
      "    17,\n",
      "    1035,\n",
      "    432,\n",
      "    2297,\n",
      "    11,\n",
      "    714,\n",
      "    429,\n",
      "    3171,\n",
      "    944,\n",
      "    1281,\n",
      "    5530,\n",
      "    369,\n",
      "    7546,\n",
      "    5256,\n",
      "    382,\n",
      "    31476,\n",
      "    807,\n",
      "    2299,\n",
      "    7274,\n",
      "    911,\n",
      "    39972,\n",
      "    367,\n",
      "    476,\n",
      "    2494,\n",
      "    770,\n",
      "    30,\n",
      "    8909,\n",
      "    304,\n",
      "    9069,\n",
      "    11,\n",
      "    330,\n",
      "    16,\n",
      "    1,\n",
      "    488,\n",
      "    330,\n",
      "    16,\n",
      "    1,\n",
      "    1410,\n",
      "    387,\n",
      "    330,\n",
      "    16,\n",
      "    16,\n",
      "    497,\n",
      "    714,\n",
      "    279,\n",
      "    3405,\n",
      "    5711,\n",
      "    5109,\n",
      "    323,\n",
      "    458,\n",
      "    16819,\n",
      "    1841,\n",
      "    11,\n",
      "    773,\n",
      "    4658,\n",
      "    537,\n",
      "    382,\n",
      "    785,\n",
      "    1196,\n",
      "    2578,\n",
      "    387,\n",
      "    57034,\n",
      "    476,\n",
      "    7497,\n",
      "    13,\n",
      "    1752,\n",
      "    2867,\n",
      "    11,\n",
      "    1052,\n",
      "    594,\n",
      "    264,\n",
      "    41185,\n",
      "    1380,\n",
      "    1251,\n",
      "    1977,\n",
      "    220,\n",
      "    16,\n",
      "    10,\n",
      "    16,\n",
      "    28,\n",
      "    5507,\n",
      "    11,\n",
      "    22023,\n",
      "    311,\n",
      "    13370,\n",
      "    11,\n",
      "    714,\n",
      "    429,\n",
      "    4977,\n",
      "    40526,\n",
      "    1431,\n",
      "    448,\n",
      "    1008,\n",
      "    15409,\n",
      "    1075,\n",
      "    72742,\n",
      "    52854,\n",
      "    382,\n",
      "    40,\n",
      "    1265,\n",
      "    2506,\n",
      "    432,\n",
      "    4285,\n",
      "    323,\n",
      "    2118,\n",
      "    13,\n",
      "    8704,\n",
      "    419,\n",
      "    374,\n",
      "    4363,\n",
      "    264,\n",
      "    22011,\n",
      "    3405,\n",
      "    504,\n",
      "    4325,\n",
      "    1101,\n",
      "    5916,\n",
      "    700,\n",
      "    476,\n",
      "    13295,\n",
      "    279,\n",
      "    31774,\n",
      "    11,\n",
      "    358,\n",
      "    3278,\n",
      "    728,\n",
      "    448,\n",
      "    279,\n",
      "    5297,\n",
      "    4226,\n",
      "    382,\n",
      "    3983,\n",
      "    438,\n",
      "    458,\n",
      "    15235,\n",
      "    11,\n",
      "    358,\n",
      "    1184,\n",
      "    311,\n",
      "    387,\n",
      "    10950,\n",
      "    323,\n",
      "    22570,\n",
      "    13,\n",
      "    10696,\n",
      "    358,\n",
      "    646,\n",
      "    912,\n",
      "    1045,\n",
      "    27385,\n",
      "    476,\n",
      "    16148,\n",
      "    421,\n",
      "    8311,\n",
      "    382,\n",
      "    2582,\n",
      "    25,\n",
      "    330,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    16819,\n",
      "    220,\n",
      "    17,\n",
      "    1189,\n",
      "    2938,\n",
      "    594,\n",
      "    2797,\n",
      "    382,\n",
      "    1249,\n",
      "    1281,\n",
      "    432,\n",
      "    803,\n",
      "    20753,\n",
      "    11,\n",
      "    358,\n",
      "    1410,\n",
      "    1977,\n",
      "    2494,\n",
      "    1075,\n",
      "    25,\n",
      "    330,\n",
      "    641,\n",
      "    6770,\n",
      "    34784,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    16819,\n",
      "    220,\n",
      "    17,\n",
      "    13,\n",
      "    2160,\n",
      "    1052,\n",
      "    264,\n",
      "    3151,\n",
      "    2266,\n",
      "    498,\n",
      "    2299,\n",
      "    7274,\n",
      "    911,\n",
      "    11974,\n",
      "    3983,\n",
      "    279,\n",
      "    3405,\n",
      "    3171,\n",
      "    944,\n",
      "    3410,\n",
      "    894,\n",
      "    2266,\n",
      "    11,\n",
      "    773,\n",
      "    358,\n",
      "    1265,\n",
      "    9214,\n",
      "    311,\n",
      "    279,\n",
      "    1429,\n",
      "    4185,\n",
      "    22845,\n",
      "    382,\n",
      "    14037,\n",
      "    9210,\n",
      "    25,\n",
      "    304,\n",
      "    1045,\n",
      "    37597,\n",
      "    11,\n",
      "    1075,\n",
      "    4621,\n",
      "    5256,\n",
      "    476,\n",
      "    738,\n",
      "    10126,\n",
      "    11,\n",
      "    714,\n",
      "    429,\n",
      "    4977,\n",
      "    916,\n",
      "    5689,\n",
      "    415,\n",
      "    1095,\n",
      "    369,\n",
      "    1741,\n",
      "    264,\n",
      "    4285,\n",
      "    3239,\n",
      "    382,\n",
      "    31476,\n",
      "    432,\n",
      "    594,\n",
      "    911,\n",
      "    2710,\n",
      "    12218,\n",
      "    30,\n",
      "    758,\n",
      "    7868,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    284,\n",
      "    220,\n",
      "    17,\n",
      "    320,\n",
      "    11284,\n",
      "    2176,\n",
      "    525,\n",
      "    830,\n",
      "    11,\n",
      "    323,\n",
      "    2726,\n",
      "    5666,\n",
      "    3171,\n",
      "    944,\n",
      "    912,\n",
      "    11,\n",
      "    773,\n",
      "    4658,\n",
      "    537,\n",
      "    3593,\n",
      "    40,\n",
      "    1744,\n",
      "    358,\n",
      "    2776,\n",
      "    7274,\n",
      "    2238,\n",
      "    1753,\n",
      "    13,\n",
      "    576,\n",
      "    1196,\n",
      "    4658,\n",
      "    24297,\n",
      "    279,\n",
      "    4226,\n",
      "    311,\n",
      "    387,\n",
      "    220,\n",
      "    17,\n",
      "    382,\n",
      "    19357,\n",
      "    5480,\n",
      "    25,\n",
      "    21806,\n",
      "    5961,\n",
      "    448,\n",
      "    330,\n",
      "    17,\n",
      "    1,\n",
      "    714,\n",
      "    8365,\n",
      "    17133,\n",
      "    432,\n",
      "    28821,\n",
      "    624,\n",
      "    151668,\n",
      "    198,\n",
      "    641,\n",
      "    6770,\n",
      "    34784,\n",
      "    11,\n",
      "    220,\n",
      "    16,\n",
      "    488,\n",
      "    220,\n",
      "    16,\n",
      "    16819,\n",
      "    3070,\n",
      "    17,\n",
      "    334,\n",
      "    13,\n",
      "    4710,\n",
      "    3872,\n",
      "    1052,\n",
      "    264,\n",
      "    3151,\n",
      "    2266,\n",
      "    320,\n",
      "    4803,\n",
      "    7868,\n",
      "    476,\n",
      "    43893,\n",
      "    34784,\n",
      "    8,\n",
      "    429,\n",
      "    498,\n",
      "    2299,\n",
      "    22023,\n",
      "    311,\n",
      "    1380,\n",
      "    419,\n",
      "    2578,\n",
      "    2297,\n",
      "    30,\n",
      "    6771,\n",
      "    752,\n",
      "    1414,\n",
      "    421,\n",
      "    498,\n",
      "    4172,\n",
      "    1075,\n",
      "    803,\n",
      "    3565,\n",
      "    0\n",
      "  ],\n",
      "  \"total_duration\": 5976938233,\n",
      "  \"load_duration\": 261587012,\n",
      "  \"prompt_eval_count\": 6,\n",
      "  \"prompt_eval_duration\": 16190626,\n",
      "  \"eval_count\": 601,\n",
      "  \"eval_duration\": 4135809190\n",
      "}\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-18T07:56:28.159955Z",
     "start_time": "2025-10-18T07:56:22.279997Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# stream\n",
    "MODEL_NAME = \"deepseek-r1:8b\"  # ollama list\n",
    "url = \"http://127.0.0.1:11434/api/generate\"\n",
    "payload = {\n",
    "    \"model\": MODEL_NAME,\n",
    "    \"prompt\": \"请介绍下自己\",\n",
    "    \"options\": {\n",
    "        \"num_predict\": 1000,\n",
    "    },\n",
    "}\n",
    "\n",
    "completion = requests.post(url, json=payload, stream=True)\n",
    "if completion.status_code == 200:\n",
    "    for line in completion.iter_lines():\n",
    "        if line:\n",
    "            try:\n",
    "               response_json = json.loads(line.decode(\"utf-8\"))\n",
    "               # print(response_json)\n",
    "               if 'response' in response_json:\n",
    "                   print(response_json['response'], end='', flush=True)\n",
    "                # if response_json.get(\"\")\n",
    "            except Exception as e:\n",
    "                print(e)\n",
    "    print(f\"res: {json.dumps(res_json, indent=2, ensure_ascii=False)}\")\n",
    "else:\n",
    "    print(f\"failed\")"
   ],
   "id": "4c25ea547e66a3de",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好呀！我是 DeepSeek-R1，一款由深度求索公司开发的人工智能助手 🤖\n",
      "\n",
      "我擅长理解和生成自然语言文字，可以帮你处理各种文本任务，比如：\n",
      "\n",
      "- 回答问题、提供建议（学习、工作、生活都可以问我哦）\n",
      "- 撰写文章、写作创意内容（小说、诗歌、演讲稿都不在话下）\n",
      "- 总结归纳长篇文章或文件内容\n",
      "- 翻译多种语言的文字\n",
      "- 代码编写与调试、技术支持答疑\n",
      "- 提供心理安慰和情感交流\n",
      "\n",
      "我支持处理各种格式的文档，比如 PDF、Word、Excel、PPT、TXT 文件等，并且能理解上下文进行深入讨论。虽然我现在还不支持图片或语音，但你可以上传文件让我阅读文字内容，我会尽力提供帮助！\n",
      "\n",
      "另外，我的知识截止到 2024 年 7 月，所以在这个时间点之前的历史事件和信息我都能掌握～不过我也在不断学习进化中，希望将来能变得更强更聪明。\n",
      "\n",
      "简单来说，我就是你的贴心智能助手，有问必答、认真负责！有什么我可以帮你的吗？😊res: {\n",
      "  \"model\": \"deepseek-r1:8b\",\n",
      "  \"created_at\": \"2025-10-18T07:56:11.230488869Z\",\n",
      "  \"message\": {\n",
      "    \"role\": \"assistant\",\n",
      "    \"content\": \"\"\n",
      "  },\n",
      "  \"done\": true,\n",
      "  \"done_reason\": \"load\"\n",
      "}\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-18T07:56:11.432479Z",
     "start_time": "2025-10-18T07:56:10.989205Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "MODEL_NAME = \"deepseek-r1:8b\"  # ollama list\n",
    "url = \"http://127.0.0.1:11434/api/chat\"\n",
    "payload = {\n",
    "    \"model\": MODEL_NAME,\n",
    "    \"message\": [\n",
    "        {\"role\": \"user\", \"content\": \"请介绍下自己\"},\n",
    "    ],\n",
    "    \"stream\": False,\n",
    "    # \"options\": {\n",
    "    #     \"num_predict\": 100000,\n",
    "    # },\n",
    "}\n",
    "\n",
    "completion = requests.post(url, json=payload)\n",
    "if completion.status_code == 200:\n",
    "    res_json = completion.json()\n",
    "    print(f\"res: {json.dumps(res_json, indent=2, ensure_ascii=False)}\")\n",
    "else:\n",
    "    print(f\"failed\")"
   ],
   "id": "195a6773abb15a32",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "res: {\n",
      "  \"model\": \"deepseek-r1:8b\",\n",
      "  \"created_at\": \"2025-10-18T07:56:11.230488869Z\",\n",
      "  \"message\": {\n",
      "    \"role\": \"assistant\",\n",
      "    \"content\": \"\"\n",
      "  },\n",
      "  \"done\": true,\n",
      "  \"done_reason\": \"load\"\n",
      "}\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "a6a7da66d64a5421"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
